package br.com.aquinoevasconcelos.servlets.arquivo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DownloadModelo
 */
@WebServlet("/DownloadModeloCliente")
public class DownloadModeloCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DownloadModeloCliente() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		
		String caminho = request.getParameter("caminho");
		
		System.out.println(caminho);
		
		try {

			File arquivo = new File(caminho);

			// tipo de arquivo
			//response.setContentType("application/pdf");
			
			//pega a extensão do arquivo
			String[] partes = arquivo.getName().split("\\.");
			int ultimaParte = partes.length - 1;
			String extensao = partes[ultimaParte];

			//seta o content type segundo a extensão
			if (extensao.equals("doc")) {
			   response.setContentType("application/msword");
			}
			if (extensao.equals("pdf")) {
			   response.setContentType("application/pdf");
			}
			if (extensao.equals("pdf")) {
			   response.setContentType("application/pdf");
			}
			if (extensao.equals("xls")) {
			   response.setContentType("application/vnd.ms-excel");
			}
			if (extensao.equals("ppt")) {
			   response.setContentType("application/vnd.ms-powerpoint");
			}
			if (extensao.equals("gzip")) {
			   response.setContentType("application/gzip");
			}
			if (extensao.equals("zip")) {
			   response.setContentType("application/zip");
			}
			if (extensao.equals("au") || extensao.equals("snd")) {
			   response.setContentType("audio/basic");
			}
			if (extensao.equals("midi")) {
			   response.setContentType("audio/midi");
			}
			if (extensao.equals("aiff")) {
			   response.setContentType("audio/x-aiff");
			}
			if (extensao.equals("wav")) {
			   response.setContentType("audio/x-wav");
			}
			if (extensao.equals("gif")) {
			   response.setContentType("image/gif");
			}
			if (extensao.equals("jpg") || extensao.equals("jpeg")) {
			   response.setContentType("image/jpeg");
			}
			if (extensao.equals("png")) {
			   response.setContentType("image/png");
			}
			if (extensao.equals("tiff")) {
			   response.setContentType("audio/tiff");
			}
			if (extensao.equals("bmp")) {
			   response.setContentType("image/x-xbitmap");
			}
			if (extensao.equals("html") || extensao.equals("htm")) {
			   response.setContentType("text/html");
			}
			if (extensao.equals("txt")) {
			   response.setContentType("text/plain");
			}
			if (extensao.equals("xml")) {
			   response.setContentType("text/xml");
			}
			if (extensao.equals("mpeg")) {
			   response.setContentType("video/mpeg");
			}

			// nome do arquivo a ser exibido na caixa de download do browser
			response.setHeader("Content-Disposition", "attachment; filename="
					+ arquivo.getName() + ";");

			// manda os bytes do arquivo pelo response
			URL url = arquivo.toURI().toURL();
			BufferedInputStream leitor = new BufferedInputStream(
					url.openStream(), 4 * 1024);
			OutputStream escritor = response.getOutputStream();
			byte[] buffer = new byte[4 * 1024];
			int size = 0;
			while ((size = leitor.read(buffer, 0, buffer.length)) != -1) {
				escritor.write(buffer, 0, size);
			}
			escritor.close();
			leitor.close();

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

}
